package com.ascent.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpURl {
    public static String getRequest(String urlP, String requestType){
        HttpURLConnection con=null;
        BufferedReader buffer=null;
        StringBuilder resultBuffer=null;
        try {
            URL url=new URL(urlP);
            try {
                con=(HttpURLConnection) url.openConnection();
                con.setRequestMethod(requestType);
                con.setRequestProperty("Content-type","application/json");
                con.setDoOutput(true);
                con.setDoInput(true);
                con.setUseCaches(false);
                if (con.getResponseCode()==HttpURLConnection.HTTP_OK){
                    InputStream inputStream=con.getInputStream();
                    resultBuffer=new StringBuilder();
                    String line;
                    buffer = new BufferedReader(new InputStreamReader(inputStream,"GBK"));
                    while ((line=buffer.readLine())!=null){
                        resultBuffer.append(line);
                    }
                    return resultBuffer.toString();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
